<?php

namespace app\api\controller\v1;

use app\api\model\CourseComment as CourseCommentModel;
use app\api\model\CourseCommentTag as CourseCommentTagModel;
use app\api\model\CourseOrder as CourseOrderModel;
use app\api\model\RemindOrder as RemindOrderModel;
use app\api\model\Tag as TagModel;
use app\api\model\TeacherCourse as TeacherCourseModel;
use app\api\validate\CourseCommentValidate;
use app\common\controller\BaseController;
use app\common\service\EasyWechatService;
use think\Db;

/**
 * 课程评价
 */

class CourseComment extends BaseController
{
    protected $noNeedLogin = ['index'];

    /**
     * 课程评价列表
     */
    public function index()
    {
        $teacherCourseId = $this->notEmpty('teacher_course_id');
        $size = $this->request->get('size');
        $comments = CourseCommentModel::getComments($teacherCourseId, $size);
        $comments ? $this->success('', $comments) : $this->notFoundData();
    }

    /**
     * 评价页面
     */
    public function read()
    {
        $courseOrderId = $this->notEmpty('course_order_id');
        $courseOrder = CourseOrderModel::getByOrderId($courseOrderId);
        if (!$courseOrder) {
            $this->errorMsg(13001);
        }
        $tag = TagModel::getByType(2);
        $this->success('', [
//            'course_order_id' => $courseOrderId,
            'course_order' => $courseOrder,
            'tag' => $tag
        ]);
    }

    /**
     * 添加课程评价
     */
    public function create(CourseCommentValidate $courseCommentValidate,
                           CourseCommentModel $courseCommentModel,
                           EasyWechatService $easyWechatService)
    {
        $this->isPost();
        $data = $this->request->post();
        $this->paramValidate($courseCommentValidate, $data);
        //检查内容是否违规
        $easyWechatService->checkText($data['content']);
        $courseOrder = CourseOrderModel::find($data['course_order_id']);
        if (!$courseOrder) {
            $this->errorMsg(13001);
        }
        if ($courseOrder->status != 3) {
            $this->errorMsg(13006);
        }
        if ($courseOrder->is_comment) {
            $this->errorMsg(13005);
        }
        $data['user_id'] = $this->userId;
        $data['teacher_course_id'] = $courseOrder->teacher_course_id;
        $data['com_score'] = ceil(($data['result_score'] + $data['service_score']) / 2);

        Db::startTrans();
        try {
            //新增评价
            $courseCommentModel->add($data);
            //更新订单为已评价
            $courseOrder->is_comment = 1;
            $courseOrder->save();
            //评论标签
            if (isset($data['tag']) && $data['tag']) {
                CourseCommentTagModel::add($courseCommentModel->course_comment_id, $data['tag']);
            }
            //增加综合评分
            $comScore = CourseCommentModel::comScore($data['teacher_course_id']);
            TeacherCourseModel::updateData($data['teacher_course_id'], ['score' => $comScore]);
            //订单提醒
            RemindOrderModel::add($courseOrder, 8);

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
        isset($e) ? $this->error($e->getMessage()) : $this->success();
    }

}
